package com.cwd.arm.domain.service.msg.impl;

import com.cwd.arm.common.enums.MsgTypeEnum;
import com.cwd.arm.domain.entity.base.MsgReEntity;
import com.cwd.arm.domain.service.msg.MsgAppService;
import com.cwd.arm.domain.util.PoToEntityUtil;
import com.cwd.arm.infrastructure.dao.ProjectDao;
import com.cwd.arm.infrastructure.dao.ReceiveNodeDao;
import com.cwd.arm.infrastructure.po.ProjectPo;
import com.cwd.arm.infrastructure.po.ReceiveNodePo;
import com.cwd.arm.infrastructure.query.ProjectDaoQuery;
import com.cwd.arm.infrastructure.query.ReceiveNodeDaoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yan on 22-2-20.
 */
@Service("RECEVICE_END")
public class ReceivceEndMsgAppServiceImpl extends MsgAppService {

    @Autowired
    private ReceiveNodeDao receiveNodeDao;

    @Autowired
    private ProjectDao projectDao;

    @Override
    protected MsgTypeEnum getMsgTypeEnum() {
        return MsgTypeEnum.RECEVICE_END;
    }

    @Override
    protected List<MsgReEntity> getMsgReEntityList() {
        List<ProjectPo> projectPoList = projectDao.getUnCloseProjectList(new ProjectDaoQuery());
        List<MsgReEntity> msgReEntityList = new ArrayList<>();
        for(ProjectPo projectPo : projectPoList){
            ReceiveNodeDaoQuery receiveNodeDaoQuery = new ReceiveNodeDaoQuery();
            receiveNodeDaoQuery.setProjectId(projectPo.getId());
            List<ReceiveNodePo> receiveNodePoList = receiveNodeDao.selectByQuery(receiveNodeDaoQuery);
            msgReEntityList.addAll(PoToEntityUtil.poToReceiveNodeEntityList(receiveNodePoList,projectPo.getProjectName()));
        }
        return msgReEntityList;
    }

    @Override
    protected MsgReEntity getMsgReEntity(Long reId) {
        ReceiveNodePo po = receiveNodeDao.getById(reId);
        return PoToEntityUtil.poToEntity(po);
    }

    @Override
    protected List<Long> getNewRecipientIdList(Long reId) {
        ReceiveNodePo receiveNodePo = receiveNodeDao.getById(reId);
        ProjectPo projectPo = projectDao.getById(receiveNodePo.getProjectId());
        return super.getNewRecipientIdList(projectPo.getId());
    }
}
